<template>
  <div>
    <el-autocomplete
      v-model="moldName"
      v-loading="loading"
      class="inline-input"
      :fetch-suggestions="selectMoldList"
      placeholder="请输入模具名称"
      style="width: 100%"
      clearable
      @select="handleSelectMold"
    >
      <template slot-scope="{ item }">
        <div>{{ item.moldName }}</div>
      </template>
      <el-button
        slot="append"
        icon="el-icon-search"
        @click="openMoldDialog"
      />
    </el-autocomplete>
    <div v-if="moldVisible">
      <mold-module ref="moldModule" :visible="moldVisible" @close="closeMoldDialog" />
    </div>
  </div>
</template>

<script>

import { selectMoldByName } from '@/api/erp/mold'
import moldModule from '@/modules/moldModule.vue'

export default {
  name: 'MoldQuery',
  components: { moldModule },
  props: {
    value: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      moldVisible: false,
      moldName: '',
      moldId: this.value,
      loading: false
    }
  },
  methods: {
    init(moldName) {
      this.moldName = moldName
    },
    resetQueryForm() {
      this.moldName = ''
      this.moldId = undefined
    },
    // 打开模具弹窗
    openMoldDialog() {
      this.moldVisible = true
      this.$nextTick(() => {
        this.$refs.moldModule.handleSearch()
      })
    },
    // 关闭模具弹窗
    closeMoldDialog(row) {
      this.moldVisible = false
      if (row) {
        this.moldName = row.moldName
      }
      this.$emit('choose', row)
    },
    // 模具选中
    handleSelectMold(row) {
      if (row) {
        this.moldName = row.moldName
        this.$emit('choose', row)
      }
    },
    selectMoldList(query, callback) {
      this.loading = true
      selectMoldByName({ moldName: query }).then(resp => {
        if (resp.success) {
          callback(resp.data)
        }
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style scoped>

</style>
